package c.e.b.h.e.k;

import android.os.Looper;
import c.e.a.b.g.g;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/* compiled from: Utils */
public final class a1 {
    public static final FilenameFilter a = new a();
    public static final ExecutorService b = c.e.a.c.a.a("awaitEvenIfOnMainThread task continuation executor");

    /* compiled from: Utils */
    public class a implements FilenameFilter {
        public boolean accept(File file, String str) {
            return true;
        }
    }

    public static <T> T a(g<T> gVar) throws InterruptedException, TimeoutException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        gVar.e(b, new z0(countDownLatch));
        if (Looper.getMainLooper() == Looper.myLooper()) {
            countDownLatch.await(4, TimeUnit.SECONDS);
        } else {
            countDownLatch.await();
        }
        if (gVar.k()) {
            return gVar.h();
        }
        if (gVar.i()) {
            throw new CancellationException("Task is already canceled");
        } else if (gVar.j()) {
            throw new IllegalStateException(gVar.g());
        } else {
            throw new TimeoutException();
        }
    }

    public static int b(File file, FilenameFilter filenameFilter, int i, Comparator<File> comparator) {
        File[] listFiles = file.listFiles(filenameFilter);
        if (listFiles == null) {
            return 0;
        }
        return c(Arrays.asList(listFiles), i, comparator);
    }

    public static int c(List<File> list, int i, Comparator<File> comparator) {
        int size = list.size();
        Collections.sort(list, comparator);
        for (File file : list) {
            if (size <= i) {
                return size;
            }
            d(file);
            size--;
        }
        return size;
    }

    public static void d(File file) {
        if (file.isDirectory()) {
            for (File d : file.listFiles()) {
                d(d);
            }
        }
        file.delete();
    }
}
